import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.sql.SQLException;

/**
 * AddEditAppointment
 * Panel with GUI to add or edit an Appointment
 * @author Bethany Sumner
 * @version 1.0
 */
public class AddEditAppointment extends JPanel {
	
	/**
	 * Field database.
	 */
	private final Database database;
	
	/**
	 * Field main.
	 */
	private final MainWindow main;
	
	/**
	 * Field appointment.
	 */
	private String appointment;
	
	/**
	 * Field comboTimes.
	 */
	private JComboBox comboTimes;
	
	/**
	 * Field btnCreate.
	 */
	private JButton btnCreate;
	
	/**
	 * Field btnUpdate.
	 */
	private JButton btnUpdate;
	
	/**
	 * Field btnCancel.
	 */
	private JButton btnCancel;
	
	/**
	 * Field txtReason.
	 */
	private JTextField txtReason;
	
	/**
	 * Field comboMonth.
	 */
	private JComboBox comboMonth;
	
	/**
	 * Field comboDay.
	 */
	private JComboBox comboDay;
	
	/**
	 * Field comboYear.
	 */
	private JComboBox comboYear;
	
	/**
	 * Field buttonListener.
	 */
	private final ButtonListener buttonListener;
	
	/**
	 * Field comboDoctor.
	 */
	private JComboBox comboDoctor;

	/**
	 * Add Appointment Constructor - used to create a new appointment
	 * @param main Reference to the main window
	 * @param database Reference to the database interface
	 */
	public AddEditAppointment(MainWindow main, Database database) {
		
		this.main = main;
		this.database = database;
		
		main.showMessage(
				"Please fill out the information below to create an appointment:");
		
		createLayout();
		
		buttonListener = new ButtonListener();
		btnCreate = new JButton("Create Appointment");
		btnCreate.setBounds(299, 231, 184, 29);
		btnCreate.addActionListener(buttonListener);
		add(btnCreate);
		
	}
	
	/**
	 * Edit Appointment Constructor - used to edit the given appointment
	 * 
	 * @param main Reference to the main window
	 * @param appointment The ID for the current appointment to edit.
	 * @param database Database
	 */
	public AddEditAppointment(MainWindow main, Database database, String appointment) {
		
		this.main = main;
		this.database = database;
		this.appointment = appointment;
		
		createLayout();
		
		main.showMessage(
				"Please fill out the information below to update the appointment:");

		// update button
		buttonListener = new ButtonListener();
		btnUpdate = new JButton("Update Appointment");
		btnUpdate.setBounds(225, 321, 184, 29);
		btnUpdate.addActionListener(buttonListener);
		add(btnUpdate);
		
	}

	/** 
	 * Add all the GUI items
	 */
	private void createLayout(){
		
		setLayout(null);
		
		// Desired Date
		final JLabel lblDate = new JLabel("Desired Date:");
		lblDate.setBounds(34, 36, 102, 16);
		add(lblDate);
		comboMonth = new JComboBox();
		comboMonth.setBounds(218, 31, 110, 27);
		comboMonth.addItem("January");
		comboMonth.addItem("Febuary");
		comboMonth.addItem("March");
		comboMonth.addItem("April");
		comboMonth.addItem("May");
		comboMonth.addItem("June");
		comboMonth.addItem("July");
		comboMonth.addItem("August");
		comboMonth.addItem("September");
		comboMonth.addItem("October");
		comboMonth.addItem("November");
		comboMonth.addItem("December");
		add(comboMonth);
		final JLabel slash1 = new JLabel("/");
		slash1.setBounds(340, 36, 14, 16);
		add(slash1);
		comboDay = new JComboBox();
		comboDay.setBounds(356, 31, 71, 27);
		for(int i = 1; i <= 31; i++){
			if(i < 10){
				comboDay.addItem("0" + i);
			}else{
				comboDay.addItem("" + i);
			}
		}
		add(comboDay);
		final JLabel slash2 = new JLabel("/");
		slash2.setBounds(439, 36, 14, 16);
		add(slash2);
		comboYear = new JComboBox();
		comboYear.setBounds(459, 31, 92, 27);
		for(int i = 0; i <= 100; i++){
			comboYear.addItem("" + (2011 - i));
		}
		add(comboYear);
		
		// available times field
		final JLabel lblAvailableTimes = new JLabel("Available Times:");
		lblAvailableTimes.setBounds(34, 76, 118, 16);
		add(lblAvailableTimes);
		comboTimes = new JComboBox();
		comboTimes.setBounds(218, 75, 250, 18);
		comboTimes.addItem("9:00 am");
		comboTimes.addItem("11:00 am");
		comboTimes.addItem("1:00 pm");
		comboTimes.addItem("3:00 pm");
		add(comboTimes);
		
		// desired doctor field
		final JLabel lblDesiredDoctor = new JLabel("Desired Doctor:");
		lblDesiredDoctor.setBounds(34, 116, 118, 16);
		add(lblDesiredDoctor);
		comboDoctor = new JComboBox();
		comboDoctor.setBounds(218, 115, 250, 18);
		comboDoctor.addItem("Dr. Matt Cooper");
		comboDoctor.addItem("Dr. Zachary Lee");
		comboDoctor.addItem("Dr. Paul Sebexen");
		comboDoctor.addItem("Dr. Bethany Sumner");
		add(comboDoctor);
		
		// reason field
		final JLabel lblReason = new JLabel("Reason for Appointment:");
		lblReason.setBounds(34, 156, 184, 16);
		add(lblReason);
		txtReason = new JTextField();
		txtReason.setBounds(218, 156, 333, 27);
		add(txtReason);
		txtReason.setColumns(10);
		
		// create ButtonListener
		final ButtonListener buttonListener = new ButtonListener();
		// make cancel button
		btnCancel = new JButton("Cancel");
		btnCancel.setBounds(114, 231, 184, 29);
		btnCancel.addActionListener(buttonListener);
		add(btnCancel);
		
	}
	
	/**
	 * Calls database and creates an appointment

	 * @throws IOException  * @throws SecurityException  */
	private void create(){
		final String date = comboDay.getSelectedItem() + " " 
			+ comboMonth.getSelectedItem() + " "
			+ comboYear.getSelectedItem();
		
		String time = "0000";
		
		if(comboTimes.getSelectedItem().equals("9:00 am")){
			time = "0900";
		}else if(comboTimes.getSelectedItem().equals("11:00 am")){
			time = "1100";
		}else if(comboTimes.getSelectedItem().equals("1:00 pm")){
			time = "1300";
		}else if(comboTimes.getSelectedItem().equals("3:00 pm")){
			time = "1500";
		}
		
		try {
			database.addAppointment(main.getCurrentUser(), date, time, txtReason.getText(), getDoctor());
			main.showAffirmation("Your appointment has been successfully created.");
			main.removeTab(this);
		} catch (MySQLIntegrityConstraintViolationException e){
			main.showError(
				"There is already an appointment at that time. Please select another time.");
			main.logError(e);
		}catch (SQLException e) {
			main.showError(
				"There was a problem creating the appointment. Please contact the System Admin.");
			main.logError(e);
			e.printStackTrace();
		} catch (SecurityException e) {
			main.logError(e);
			main.showError(
					"There was a problem creating the appointment. Please contact the System Admin.");
			e.printStackTrace();
		} 
		
		
	}
	
	/**
	 * Method gets the username of the doctor selected
	 * @return The user name of the doctor selected
	 */
	private String getDoctor(){
		
		String doctor;
		
		if(comboDoctor.getSelectedItem().equals("Dr. Bethany Sumner")){
			doctor = "bsumner";
		}else if(comboDoctor.getSelectedItem().equals("Dr. Zachary Lee")){
			doctor = "zlee";
		}else if(comboDoctor.getSelectedItem().equals("Dr. Matt Cooper")){
			doctor = "mcooper";
		}else{
			doctor = "psebexen";
		}
		
		return doctor;
		
	}
	
	/**
	 * Cancels the adding or updating of the appointment and closes the tab
	 */
	private void cancel(){
		main.removeTab(this);
		main.clearMessage();
	}
	
	/**
	 * Gets the Current Appointment ID
	
	 * @return The appointment that is currently being edited */
	public String getAppointment(){
		return this.appointment;
	}

	/**
	 * Returns a string representation of AddEditAppointment
	
	 * @return A string representation of AddEditAppointment */
	@Override
	public String toString() {
		return "AddEditAppointment [txtReason=" + txtReason + "]";
	}

	/**
	 * ButtonListener
	 * ActionListener to listen when button has been pressed
	 * @author Bethany Sumner
	 *
	 */
	
	private class ButtonListener implements ActionListener{
		
		/**
		 * Performs tasks corresponding to the action
		 * @param event The action event
		
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
		public void actionPerformed(ActionEvent event){
			
			if(event.getSource().equals(btnCreate)){
				create();
			}else if(event.getSource().equals(btnCancel)){
				cancel();
			}
			
		}

	}
}